package com.horoscope.astrology.zodiac.signs.free.fragment.article

import android.os.Bundle
import android.util.SparseArray
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.horoscope.astrology.zodiac.signs.free.activity.QuizzesActivity
import com.horoscope.astrology.zodiac.signs.free.adapter.QuizzesListAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.bean.QuizResultBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.QuizzesDecoration
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil
import kotlinx.android.synthetic.main.fragment_base_swipe_refresh.*

/**
 * Article 页 具体的四个Fragment
 * */
class QuizzesSubFragment : BaseSwipeRefreshFragment() {

    private var mAdapter: QuizzesListAdapter? = null
    private var mLinearLayoutManager: LinearLayoutManager? = null
    private var title = ""
    private var interval = 0
    private var list = ArrayList<QuizBean>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        list.clear()
        title = arguments?.getString("title")!!
        mLinearLayoutManager = LinearLayoutManager(context)
        mRecyclerView.layoutManager = mLinearLayoutManager
        mAdapter = QuizzesListAdapter(activity)
        mRecyclerView.adapter = mAdapter
        mRecyclerView.addItemDecoration(QuizzesDecoration(15f, 10f))
        mAdapter?.setTitleOfFragment(title)

    }

    fun notifyAdapter() {
        mAdapter?.notifyDataSetChanged()
    }

    companion object {
        val sparseArray = SparseArray<String>()
        fun title(position: Int): String {
            sparseArray.put(0, "Newest")
            sparseArray.put(1, "Hottest")
            return sparseArray[position]
        }
    }

    // 下拉加载
    override fun requestData() {
        super.requestData()
        include_error.visibility = View.GONE
        NetworkEngine.getQuiz(
            title,
            (currentPage++).toString(),
            object : NetworkListener<QuizResultBean>() {
                override fun onSuccess(t: QuizResultBean) {
                    if (isAdded) {
                        list = t.data
                        include_error.visibility = View.GONE
                        interval = 0 // 间隔
                        mAdapter?.setData(t.data)
                        header?.setSuccess(true)
                        mPtrFrameLayout?.refreshComplete()
                    }
                }

                override fun onFailed(t: QuizResultBean) {
                    super.onFailed(t)
                    errorMake()
                }

                override fun onError(e: String?) {
                    super.onError(e)
                    errorMake()
                }

            })
    }

    private fun errorMake() {
        if (isAdded) {
            currentPage--
            header?.setSuccess(false)
            mPtrFrameLayout?.refreshComplete()

            if (isAdded && !isHidden && list.size != 0) {
                ToastUtil.showRewardError(activity)
            } else {
                include_error.visibility = View.VISIBLE
            }
        }
    }

    // 上拉加载
    override fun loadMore() {
        super.loadMore()
        NetworkEngine.getQuiz(
            title,
            (currentPage++).toString(),
            object : NetworkListener<QuizResultBean>() {
                override fun onSuccess(t: QuizResultBean) {
                    if (isAdded) {
                        if (t.data.isEmpty()) {
                            mAdapter?.setLoadFinished(true)
                        }
                        (activity as QuizzesActivity).showLoaded()
                        mAdapter?.setMoreData(t.data)
                    }
                }

                override fun onFailed(t: QuizResultBean) {
                    super.onFailed(t)
                    loadMoreError()
                }

                override fun onError(e: String?) {
                    super.onError(e)
                    loadMoreError()
                }

            })
    }

    private fun loadMoreError() {
        if (isAdded && !isHidden) {
            currentPage--
            ToastUtil.showRewardError(activity)
        }
    }
}